﻿using Frame.Core.Common.Common;
using Frame.Core.Common.Extensions;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;

namespace Frame.Core.Common.Auth
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class User : IUser
    {
        private readonly IHttpContextAccessor _accessor;

        public User(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        /// <summary>
        /// 用户Id
        /// </summary>
        public virtual int Id
        {
            get
            {
                var id = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.Sid);
                if (id != null && id.Value.NotNull())
                {
                    return id.Value.ToInt();
                }
                return 0;
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string Name
        {
            get
            {
                var name = _accessor.HttpContext.User.Identity.Name;

                return name;
            }
        }

        /// <summary>
        /// 昵称
        /// </summary>
        public string ChineseName
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.GivenName);

                if (name != null && name.Value.NotNull())
                {
                    return name.Value;
                }

                return "";
            }
        }


        /// <summary>
        /// 任务角色
        /// </summary>
        public string TaskRoleId
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.Role);

                if (name != null && name.Value.NotNull())
                {
                    return name.Value;
                }

                return "";
            }
        }

        /// <summary>
        /// 所属部门
        /// </summary>
        public string DeptId
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.GroupSid);

                if (name != null && name.Value.NotNull())
                {
                    return name.Value;
                }

                return "";
            }
        }
    }
}
